using System;
using System.Collections;
using System.Html;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;
using Yoshi.Classes;
using YoshiBase.Classes;
using jQueryApi;

namespace YesPlayLove.Clusters
{
    [IgnoreNamespace]
    public class CommentBubble : EditBase
    {
        private readonly string _itemId;

        private CommentBubble(Await awp, string itemId, jQueryObject assocEl)
        {
            _itemId = itemId;
            AwpIn = awp;

            Element = jQuery.FromHtml("<div class='CommentBubbleEdit EditBubble'/>")
                .CSS(
                    new Dictionary(
                        "top", assocEl.GetOffset().Top + assocEl.GetOuterHeight(),
                        "left", assocEl.GetOffset().Left
                        )
                )
                .Bind("keydown.esc", delegate(jQueryEvent e) { Keys.EscKey(e, Cancel); })
                .Bind("keydown.shiftret", delegate(jQueryEvent e) { Keys.ShiftRetKey(e, Ok); })
                ;

            BuildCommon();

            OkBtn.Html(ThemeBase.CommentAddBtnFace);
            CancelBtn.Html(ThemeBase.CancelBtnFace);

            Element.AppendTo(Document.Body);
            new Await()
                .AddAw(ScrollInViewAw, Element)
                .AddAw(FocusElementAw, Textarea)
                .Commit();
            Hider(true, 0.35);
        }

        protected override void Ok(jQueryEvent e)
        {
            CommentData dta = new CommentData();
            string msg = Textarea.Plugin<Jewl>().GetFValueString();
            if (!String.IsNullOrEmpty(msg))
                dta.WriteCommentAw(Await.AsyncAw, _itemId, msg);
            // ReSharper disable RedundantBaseQualifier
            base.Ok(); // base. must be there or javascript recurs
            // ReSharper restore RedundantBaseQualifier
        }

        /// <summary>
        /// Creates an animated comment edit bubble.
        /// </summary>
        /// <param name="awp">The parent await object</param>
        /// <param name="itemId">The id of the item being commented on.</param>
        /// <param name="assocEl">The element this is associated with and positioned to</param>
        public static void NewAw(Await awp, string itemId, jQueryObject assocEl)
        {
            new CommentBubble(awp, itemId, assocEl);
        }

        public static void InsertCommentAw(Await awp, jQueryObject hostEl, object commentItem)
        {
            CommentItem item = (CommentItem) commentItem;

            jQueryObject commentBody = jQuery.FromHtml("<p class='Comment'/>")
                .AppendTo(hostEl)
                .Html(item.blob)
                ;

            jQueryObject usrImg = jQuery.FromHtml("<img class='ByImg'/>")
                .Attribute("src", ThemeBase.UnknownUserPhoto)
                .PrependTo(commentBody)
                ;

            jQueryObject usrName = jQuery.FromHtml("<span class='ByName'/>")
                .InsertAfter(usrImg)
                ;

            new Await()
                .AddAw(ProfileData.GetUserByIdAw, item.byId)
                .HandleDl(Exceptions.BlobLoadError, Await.AbortAw)
                .AddDx(
                    delegate(Await aw)
                    {
                        YoshiUser yu = (YoshiUser) aw[PkgBase.ResultRx];
                        string name = Script.Value(yu.alias, yu.name);
                        usrImg.Attribute("src", Script.Value(yu.photo, ThemeBase.UnknownUserPhoto));
                        usrImg.Attribute("title", name);
                        usrName.Html(name)
                            .Click(delegate { new Ask().OkAw(Await.AsyncAw, "We haven't dreamed user pages into manifestation yet!!\r\nTry again soon cause Natalie is on it " + ThemeBase.Smile); })
                            ;
                        usrName.After(
                            jQuery.FromHtml("<span> " + ThemeBase.Says + " - </span>")
                            );
                    })
                .Commit(awp);
        }
    }
}